package com.work.formaldehyde.activityadapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.KeFuActivity;
import com.work.formaldehyde.model.AllDingDanModel;
import com.work.formaldehyde.model.DZFDingDanModel;
import com.work.formaldehyde.util.ApiUtils;

import java.util.ArrayList;

public class DaiZhiFuAdapter extends BaseAdapter {
    ArrayList<AllDingDanModel.data> list;
    Context context;
    private LayoutInflater mInflater;
    private DetailViewHolderListener mListener;

    public DaiZhiFuAdapter(ArrayList<AllDingDanModel.data> list, Context context, DetailViewHolderListener mListener) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.mListener = mListener;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.dfk_list_item, null);
        ApiUtils.SetLog("position="+position);
        init i = new init();
        i.quxiaodingdan = convertView.findViewById(R.id.quxiaodingdan);
        i.quzhifu = convertView.findViewById(R.id.quzhifu);
        i.yiquxiao = convertView.findViewById(R.id.yiquxiao);
        i.head = convertView.findViewById(R.id.head);
        i.shangpingmingzi = convertView.findViewById(R.id.shangpingmingzi);
        i.gouwuche_manjian_text = convertView.findViewById(R.id.gouwuche_manjian_text);
        i.peisongdizhi = convertView.findViewById(R.id.peisongdizhi);
        i.yuyueshijian = convertView.findViewById(R.id.yuyueshijian);
        i.gouwuche_img = convertView.findViewById(R.id.gouwuche_img);
        i.zixunkf=convertView.findViewById(R.id.zixunkf);
        i.zixunkf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent tokf = new Intent(context, KeFuActivity.class);
                context.startActivity(tokf);
            }
        });
        i.yuyueshijian.setText(list.get(position).getStart_time() + "至" + list.get(position).getEnd_time());
        i.peisongdizhi.setText(list.get(position).getUser_address());
        ApiUtils.SetLog("msg01:" + list.get(position).getGoods().get(0).getGoods_img());
        ApiUtils.GET_IMAGE(context, list.get(position).getGoods().get(0).getGoods_img(), i.gouwuche_img);
        i.shangpingmingzi.setText(list.get(position).getGoods().get(0).getGoods_name());
        i.gouwuche_manjian_text.setText("¥" + list.get(position).getGoods().get(0).getGoods_discount_price());
        if(list.get(position).getGoods().size()>1){
            for(int creview=1;creview<list.get(position).getGoods().size();creview++) {
                LinearLayout relativeLayout = new LinearLayout(context);
                LinearLayout relativeLayouts = new LinearLayout(context);
                ImageView imageView = new ImageView(context);
                TextView textView = new TextView(context);
                TextView textViews = new TextView(context);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lpimg = new LinearLayout.LayoutParams(i.gouwuche_img.getLayoutParams().width, i.gouwuche_img.getLayoutParams().height);
                LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lps.setMargins(24, 0, 0, 0);
                lp1s.setMargins(4, 4, 4, 4);
                lp.setMargins(14, 14, 0, 0);
                lpimg.setMargins(14, 0, 0, 0);
                lp1.setMargins(0, 12, 0, 0);
                relativeLayout.setLayoutParams(lp);
                relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
                relativeLayouts.setLayoutParams(lps);
                relativeLayouts.setOrientation(LinearLayout.VERTICAL);
                imageView.setLayoutParams(lpimg);
                ApiUtils.GET_IMAGE(context, list.get(position).getGoods().get(creview).getGoods_img(), imageView);
                textView.setLayoutParams(lp1);
                textView.setLines(2);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                textView.setText( list.get(position).getGoods().get(creview).getGoods_name());
                textView.setTextColor(context.getResources().getColor(R.color.text_black));
                textView.setTextSize(14);
                textViews.setLayoutParams(lp1);
                textViews.setText("¥" + list.get(position).getGoods().get(creview).getGoods_discount_price());
                textViews.setTextColor(context.getResources().getColor(R.color.home_bottom_text_color_ischeck));
                textViews.setTextSize(11);
                relativeLayout.addView(imageView);
                relativeLayout.addView(relativeLayouts);
                relativeLayouts.addView(textView);
                relativeLayouts.addView(textViews);
                i.head.addView(relativeLayout);
            }
        }
        mListener.setData(i, position);
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        public LinearLayout head,zixunkf;
        public TextView shangpingmingzi, gouwuche_manjian_text, yuyueshijian, peisongdizhi;
        public ImageView gouwuche_img;
        public RelativeLayout quxiaodingdan,quzhifu,yiquxiao;
    }

    /**
     * 展示不同数据的接口
     */
    public interface DetailViewHolderListener {
        void setData(init viewHolder, int position);
    }
}

